<?php
require_once dirname(__FILE__) . '/InterfaceBase.php';
class AuthenInterface extends InterfaceBase implements IService
{
    # 内测属性  1:添加，0删除
    const TEST_ACCOUNTS_ADD = 1;
    const TEST_ACCOUNTS_DELETE = 0;
    # 信任IP操作 1:添加，0删除
    const TRUST_IP_ADD = 1;
    const TRUST_IP_DELETE = 0;
    # 认证状态 1:测试，2:上线
    const AUTHEN_STATUS_TEST = 1;
    const AUTHEN_STATUS_ONLINE = 2;
    # CAS白名单 1:添加,0:删除
    const CAS_ADD = 1;
    const CAS_DELETE = 0;
    
    public function __construct ($env = self::APP_ENV_TEST)
    {
        parent::__construct($env);
        $this->loadSdk("SDOServiceAuthen");
    }
    /**
     * 开通认证服务
     * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
     * @param array $args array(
     * appId=>应用ID,
     * app=>array('appName'=>'应用名称', 'test'=>测试状态)
     * cas=>array('domain'=>'域名'（可为数组）, 'isSelf'=>是否开启自身防沉迷|boolean)
     * )
     * @param int $env 环境变量
     * @return boolean
     */
    public function open ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        /*if (SDOServiceAuthen::searchApp($msg, $args['appId'], self::$ENVType[$env])) {
            $message = $this->getErrMsg(-20002001, "APP EXISTS");
        	return true;
        }*/
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        if (! isset($args['app'])) {
            $this->setError(SDOServiceErrorCode::API_EC_PARAM, "Invalid Pamaters");
            $this->endLog(__CLASS__);
            return false;
        }
        $appInfo = $args['app'];
        $this->processLog(__CLASS__, 'addApp');
        if (! SDOServiceAuthen::addApp($msg, $appId, $appInfo['appName'], $appInfo['test'], $sdkEnv)) {
            $this->setError(SDOServiceAuthen::getErrNo(), $msg);
            $this->endLog(__CLASS__);
            return false;
        }
        if (isset($args['cas'])) {
        	$this->processLog(__CLASS__, 'setCasDomain');
            $casInfo = $args['cas'];
            if (! isset($casInfo['isSelf'])) {
                $casInfo['isSelf'] = false;
            }
            if (!is_array($casInfo['domain'])) {
            	$casList = array($casInfo['domain']);
            } else {
            	$casList = $casInfo['domain'];
            }
            foreach ($casList as $casDomain) {
	            if (! SDOServiceAuthen::setCasDomain($msg, $appId, $casDomain, $casInfo['isSelf'], 
	            $sdkEnv)) {
	                $this->setError(SDOServiceAuthen::getErrNo(), $msg);
	                $this->endLog(__CLASS__);
	                return false;
	            }
            }
            unset($casList);
            unset($casInfo); 
            
        }
        $this->endLog(__CLASS__);
        return true;
    }
    /**
     * 关闭认证服务
     * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
     * @param array $args array(
     * appId=>应用ID,
     * cas=>array('domain'=>'域名'（可为数组）)
     * )
     * @param int $env 环境变量
     * @return boolean
     */
    public function close ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        if (isset($args['cas'])) {
        	$this->processLog(__CLASS__, 'removeCasDomain');
            $casInfo = $args['cas'];
        	if (!is_array($casInfo['domain'])) {
            	$casList = array($casInfo['domain']);
            } else {
            	$casList = $casInfo['domain'];
            }
            foreach ($casList as $casDomain) {
	            if (! SDOServiceAuthen::removeCasDomain($msg, $appId, $casDomain, $sdkEnv)) {
	                $this->setError(SDOServiceAuthen::getErrNo(), $msg);
	                $this->endLog(__CLASS__);
	                return false;
	            }
            }
            unset($casList);
            unset($casInfo);            
        }
        $this->endLog(__CLASS__);
        return true;
    }
    /**
     * 更新认证服务
     * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
     * @param array $args array(
     * appId=>应用ID,
     * app=>array('appName'=>'应用名称', 'test'=>测试状态),
     * cas=>array('domain'=>'域名'（可为数组）, 'isSelf'=>是否开启自身防沉迷|boolean)
     * )
     * @param int $env 环境变量
     * @return boolean
     */
    public function update ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        /*if (SDOServiceAuthen::searchApp($msg, $args['appId'], self::$ENVType[$env])) {
            $message = $this->getErrMsg(-20002001, "APP EXISTS");
        	return true;
        }*/
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        if (isset($args['app'])) {
        	$this->processLog(__CLASS__, 'editAppStatus');
            $appInfo = $args['app'];
//            if (! SDOServiceAuthen::addApp($msg, $appId, $appInfo['appName'], $appInfo['test'], 
//            $sdkEnv)) {
//                $this->setError(SDOServiceAuthen::getErrNo(), $msg);
//                return false;
//            }
//        }
//        if (isset($args['appStatus'])) {
//            $appStatus = $args['appStatus'];
            if ($env == self::APP_ENV_PRODUCTION && ! SDOServiceAuthen::editAppStatus($msg, $appId, 
            $appInfo['test'], $sdkEnv)) {
                $this->setError(SDOServiceAuthen::getErrNo(), $msg);
                $this->endLog(__CLASS__);
                return false;
            }
        }
        if (isset($args['cas'])) {
        	$this->processLog(__CLASS__, 'setCasDomain');
            $casInfo = $args['cas'];
            if (! isset($casInfo['isSelf'])) {
                $casInfo['isSelf'] = false;
            }
            if (!is_array($casInfo['domain'])) {
            	$casList = array($casInfo['domain']);
            } else {
            	$casList = $casInfo['domain'];
            }
            foreach ($casList as $casDomain) {
	            if (! SDOServiceAuthen::setCasDomain($msg, $appId, $casDomain, $casInfo['isSelf'], 
	            $sdkEnv)) {
	                $this->setError(SDOServiceAuthen::getErrNo(), $msg);
	                $this->endLog(__CLASS__);
	                return false;
	            }
            }
            unset($casList);
            unset($casInfo); 
        }
        $this->endLog(__CLASS__);
        return true;
    }
    /**
     * 查询服务信息
     * @param array $args array(
     * 	appId=>应用ID,
     *  cas=>array(域名1, www.sdo.com),
     *  testAccount=>array('account'=>测试账号, 'areaId'=>区组)
     * )
     * @param int $env
     * @return array(appInfo=>应用信息,cas=>cas信息,testAccount=>是否有权限)|boolean
     */
    public function get ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        
        $result = array('appInfo' => array());
        if ( $env==self::APP_ENV_PRODUCTION ) {
        	$this->processLog(__CLASS__, 'searchApp');
        	$tmp = SDOServiceAuthen::searchApp($msg, $appId, $sdkEnv);
        	if ($tmp===false) {
        		$this->setError(SDOServiceAuthen::getErrNo(), $msg);
        		$this->endLog(__CLASS__);
        		return false;
        	}
        	$result['appInfo'] = array('appId'=>$tmp[1], 'appName'=>$tmp[2], 'test'=>$tmp[3]);
        	if ($result['appInfo']['test']!=self::AUTHEN_STATUS_TEST) {
        		$result['appInfo']['test'] = self::AUTHEN_STATUS_ONLINE;
        	}
        	unset($tmp);
        }
        if (isset($args['cas'])) {
        	$this->processLog(__CLASS__, 'queryCasDomain');
        	$cas = $args['cas'];
        	$result['cas'] = array();
        	foreach($cas as $domain) {
        	    $result['cas'][$domain] = SDOServiceAuthen::queryCasDomain($msg, $domain, $sdkEnv);        	    
        	}
        }
        if (isset($args['testAccount'])) {
            if ($env==self::APP_ENV_PRODUCTION) {
            	$result['testAccount'] = true;
            } else {
                $testAccount = $args['testAccount'];
            	$result['testAccount'] = SDOServiceAuthen::QueryPermissionUserGame($msg, $testAccount['account'], $appId, $testAccount['areaId'], $sdkEnv);
            }   
        }
        $this->endLog(__CLASS__);
        return $result;
    }
    /**
     * 
     * @param array $message
     * @param array $args array(
     * 'appId'=>应用ID,
     * 'testAccounts'=>array(
     * array('op'=>AuthenInterface::TEST_ACCOUNTS_DELETE, 'account'=>测试账号, 'areaId'=>区组),
     * array('op'=>AuthenInterface::TEST_ACCOUNTS_ADD, 'account'=>mobfan, 'areaId'=>1)
     * ),
     * 'trustIp'=>array(
     * array('ip'=>127.0.0.1,areaId=>1,'groupId'=>1, 'op'=>AuthenInterface::TRUST_IP_DELETE),
     * array('ip'=>127.0.0.1,areaId=>1,'groupId'=>1, 'op'=>AuthenInterface::TRUST_IP_ADD),
     * ),
     * cas=>array(
     * 	domain=>array(
     *  	array('domain'=>kaka.com, 'op'=>AuthenInterface::CAS_ADD),
     *  	array('domain'=>abc.com, 'op'=>AuthenInterface::CAS_DELETE),
     * 	),
     * isSelf=>是否开启自身防沉迷|boolean
     * )
     * )
     * @param mixed $env
     */
    public function sync ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        if (isset($args['testAccounts']) && $env == self::APP_ENV_PRODUCTION) {
        	$this->processLog(__CLASS__, 'testAccounts');
            $testAccounts = $args['testAccounts'];
            foreach ($testAccounts as $row) {
                if (! SDOServiceAuthen::ModifyPermissionUserGame($msg, $row['account'], 
                $appId, $row['areaId'], $row['op'], $sdkEnv)) {
                    $this->setError(SDOServiceAuthen::getErrNo(), $msg);
                    $this->endLog(__CLASS__);
                    return false;
                }
            }
        }
        if (isset($args['trustIp']) && $env == self::APP_ENV_PRODUCTION) {
        	$this->processLog(__CLASS__, 'trustIp');
            $trustIps = $args['trustIp'];
            foreach ($trustIps as $row) {
                if ($row['op'] == self::TRUST_IP_ADD && ! SDOServiceAuthen::addTrustIP(
                $msg, $row['ip'], $appId, $row['areaId'], $row['groupId'], $sdkEnv)) {
                    $this->setError(SDOServiceAuthen::getErrNo(), $msg);
                    $this->endLog(__CLASS__);
                    return false;
                }
            }
        }
        if (isset($args['cas'])) {
        	$this->processLog(__CLASS__, 'cas');
        	$casInfo = $args['cas'];
        	if (! isset($casInfo['isSelf'])) {
                $casInfo['isSelf'] = false;
            }
            if (!is_array($casInfo['domain'])) {
            	$casList = array($casInfo['domain']);
            } else {
            	$casList = $casInfo['domain'];
            }
            foreach ($casList as $casRow) {
	            if ($casRow['op']==self::CAS_DELETE) {
	            	if (! SDOServiceAuthen::removeCasDomain($msg, $appId, $casRow['domain'], $sdkEnv)) {
	            		$this->setError(SDOServiceAuthen::getErrNo(), $msg);
		                $this->endLog(__CLASS__);
		                return false;
	            	}	                
	            } else {
	            	if (! SDOServiceAuthen::setCasDomain($msg, $appId, $casRow['domain'], $casInfo['isSelf'], 
	            $sdkEnv)) {
	            		$this->setError(SDOServiceAuthen::getErrNo(), $msg);
		                $this->endLog(__CLASS__);
		                return false;
	            	}	
	            }
            }
        }
        $this->endLog(__CLASS__);
        return true;
    }
}